package cn.com.generaldata.os.ws.impl;

import javax.jws.WebService;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

import cn.com.generaldata.os.builder.PacketRsBuilder;
import cn.com.generaldata.os.common.CommonPartVidate;
import cn.com.generaldata.os.common.DataSourseChance;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.pojo.mvc.Report;
import cn.com.generaldata.os.result.PacketRs;
import cn.com.generaldata.os.service.common.ReportedDataService;
import cn.com.generaldata.os.service.intf.ReportServiceInterface;
import cn.com.generaldata.os.util.TransitionTool;
import cn.com.generaldata.os.vo.IdentityVO;
import cn.com.generaldata.os.ws.intf.ReportedDataWSInterface;
import cn.com.generaldata.os.ws.intf.WsConstants;

@WebService(serviceName = "reportedDataWebService", portName = "ReporteDataWSInterfacePort", endpointInterface = "cn.com.generaldata.os.ws.intf.ReportedDataWSInterface", targetNamespace = WsConstants.NS)
public class ReportedDataWS<T> implements ReportedDataWSInterface<T> {

	private static final Logger logger = Logger.getLogger(ReportedDataWS.class);

	private ReportedDataService reportedDataService;
	private ReportServiceInterface reportService;

	public void setReportService(ReportServiceInterface reportService) {
		this.reportService = reportService;
	}

	/**
	 * 企业信息操作
	 */
	@SuppressWarnings("static-access")
	@Override
	public PacketRs BusinessInfor(String xml) {
		CommonPartVidate commonPartVidate = new CommonPartVidate();
		// String filePath = null;
		// if(serviceId.equals("11001")){
		// filePath = getpath()+ "/xsd/zjqyxxqq";
		// }else if(serviceId.equals("11002")){
		// filePath = getpath()+ "/xsd/zjqyxxqq";
		// }
		IdentityVO i = commonPartVidate.commonVidate(xml);
		String serviceId = i.getServiceId();
		String channelId = i.getChannelId();
		String cData = getCdata(xml);
		// boolean check = new XMLCheck().Xml2Xsd(cData, filePath);
		PacketRsBuilder builder = new PacketRsBuilder();
		// if(check)
		// {
		try {
			User user = TransitionTool.fromXML(cData, User.class);
			reportedDataService.BusinessInfor(user, serviceId, channelId);
			builder.buildSuccessResult();
		} catch (Exception e) {
			e.printStackTrace();
			builder.buildErrorResult(e);
		}
		// }
		return builder.getPacketRs();
	}

	/**
	 * 数据上报操作
	 */
	@Override
	public PacketRs ReportData(String xml) {
		CommonPartVidate commonPartVidate = new CommonPartVidate();
		// String filePath = null;
		IdentityVO i = commonPartVidate.commonVidate(xml);
		String serviceId = i.getServiceId();
		String channelId = i.getChannelId();
		// if(serviceId.equals("11003")||serviceId.equals("11003")){
		// filePath = getpath()+ "/xsd/sjsbqq.xsd";
		// }
		String cData = getCdata(xml);
		// boolean check = new XMLCheck().Xml2Xsd(cData, filePath);
		PacketRsBuilder builder = new PacketRsBuilder();
		// if(check)
		// {
		try {
			Report report = TransitionTool.fromXML(cData, Report.class);
			DataSourseChance.dataChance(channelId);
			reportedDataService.ReportData(report, serviceId, channelId);
			builder.buildSuccessResult();
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			builder.buildErrorResult(e);
		}
		// }
		logger.info("ws调用成功, xml为: " + xml);
		return builder.getPacketRs();
	}

	/**
	 * 数据同步
	 */
	@SuppressWarnings("static-access")
	@Override
	public PacketRs synchronizationData(String xml) {

		String cData = getCdata(xml);
		PacketRsBuilder builder = new PacketRsBuilder();
		try {
			Report report = TransitionTool.fromXML(cData, Report.class);
			reportedDataService.synchronizationData(report, xml);
			builder.buildSuccessResult();
		} catch (Exception e) {
			e.printStackTrace();
			builder.buildErrorResult(e);
		}
		return builder.getPacketRs();
	}

	public void setReportedDataService(ReportedDataService reportedDataService) {
		this.reportedDataService = reportedDataService;
	}

	/**
	 * 获取xsd路径
	 * 
	 * @return
	 */
	public String getpath() {
		String path = ReportedDataWS.class.getClass().getResource("/").getPath().substring(1).replace("classes/", "");
		return path;
	}

	/**
	 * 获取CDATA内容
	 * 
	 * @param xml
	 * @return
	 */
	public String getCdata(String xml) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(xml);
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}
		Node node = document.selectSingleNode("package/businessContent/subPackage/content");
		return node.getText();
	}
}
